angular.module('starter')
.controller("searchCtrl", function ($scope,$state,httpService,appBase, searchData) {
    $scope.historyBol = false;
    $scope.searchCheck = true;
	$scope.emptyHistory = function(){//清空记录按钮
	    $scope.historyBol = true;
	    searchData.clear();
	    $scope.searchHistory = [];
	}
	
	//搜索
	$scope.searchHistory = searchData.keys;//搜索历史
	$scope.hotDoctor = [];//推荐医生
	$scope.ItemArr2 = []; //搜索后页面遍历显示的数组 
	$scope.searchCont = {};//搜索内容
	$scope.replaceloadhotDoctor = function () {
	    loadhotDoctor(4);
	};
	
	$scope.inputSearch = function () {//input的change事件
	    if (!$scope.searchCheck)
	    {
	        return;
	    }
	    $scope.ItemArr2 = []; //每次搜索先清空数组内容 
	    var searchValue = $scope.searchCont.key;
	    if (searchValue.length < 2)
	    {
	        return;
	    }

	    loadSearchData();
	}
	$scope.clearBtn = function(){
		$scope.searchCont = {};  
    	$scope.ItemArr2 = [];
	}

    ///跳转页面
	$scope.searchToUrl = function (item)
	{
	    if (item.type == 0)
	    {
	        return;
	    }
	    searchData.add($scope.searchCont.key)
	    $scope.searchHistory = searchData.keys;
	    $scope.historyBol = false;
	    if (item.type == 1)
	    {
	        $state.go('selectOffice', { hopId: item.hspcode, hopName: item.hspname, addr: item.addr });
	    }
	    if (item.type == 2)
	    {
	        appBase.hospitalCode = item.hspcode;
	        appBase.hospitalName = item.hspname;
	        appBase.addr = item.addr;
	        $state.go('doctorHome', { docId: item.docId, detpId: item.detpId });
	    }
	    return;
	}

	$scope.$watch('$viewContentLoaded', function () {
	    // 你的代码
	    loadhotDoctor(4);
	});

    //加载搜索
	function loadSearchData(isInIt) {
	    isInIt = arguments[0] || false;
	    if (isInIt) {
	    }
	    $scope.searchCheck = false;
	    $scope.ItemArr2 = [];
	    var parData = "?key=" + $scope.searchCont.key + "&cityCode=" + appBase.cityCode;
	    httpService.get(httpService.appWebApi.getSearchData + parData
          ).then(
          function (success) {
              var data = success.data;
              if (data.returnCode != "0") {
                  console.log(data.returnMsg)
                  return;
              }
              angular.forEach(data.returnData, function (modal) {
                  $scope.ItemArr2.push(
                      {
                          "hspname": modal.hspname,
                          "name": modal.name,
                          "hspcode": modal.hspcode,
                          "addr": modal.addr,
                          "detpId": modal.detpId,
                          "docName": modal.docName,
                          "docId": modal.docId,
                          "type": modal.type
                      });
              });
          }
          , function (error) {
              console.log(error);
          }).finally(function () {
              $scope.searchCheck = true;
              if ($scope.ItemArr2 == '') {
                  $scope.ItemArr2.push({
                  	  "name": "未找到相关内容",
                      "hspname": "",
                      "hspcode": "",
                      "addr": "",
                      "detpId": "",
                      "docName": "",
                      "docId": "",
                      "type": 0
                  });
              }
          });
	}

    //推荐医生数据
	function loadhotDoctor(top) {
	    $scope.hotDoctor = [];
 
	    var parData = "?page=" + top + "&cityCode=" + appBase.cityCode;
	    httpService.get(httpService.appWebApi.getHotDoctor + parData
          ).then(
          function (success) {
              var data = success.data;
              if (data.returnCode != "0") {
                  console.log(data.returnMsg)
                  return;
              }
              angular.forEach(data.returnData, function (modal) {
                  $scope.hotDoctor.push(
                      {
                          "name": modal.doctorName,
                          "deptName": modal.deptName,
                          "hspcode": modal.hsp_Code,
                          "hspname": modal.hospitalName,
                          "pic": modal.urlPic || "img/face1.png",
                          "addr": modal.addr,
                          "detpId": modal.deptId,
                          "docName": modal.doctorName,
                          "docId": modal.doctorId,
                          "type": 2
                      });
              });
          }
          , function (error) {
              console.log(error);
          }).finally(function () {
              if ($scope.hotDoctor == '') {
                  $scope.hotDoctor.push({
                      "name": "未找到相关内容",
                      "deptName": "",
                      "pic": modal.urlPic || "img/face1.png",
                      "hspcode": "",
                      "hspname": "", 
 					  "addr": "",
                      "detpId": "",
                      "docName": "",
                      "docId": "",
                      "type": 0
                  });
              }
          });
	}

})